package com.hylanda.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.hylanda.sys.dao.DepartmentRepository;
import com.hylanda.sys.entity.Department;
import com.hylanda.sys.model.DepartmentQo;

@Service("departmentService")
public class DepartmentService {
    
    @Autowired
    private DepartmentRepository departmentRepository;

    public Department findById(Long id) {
        return departmentRepository.findOne(id);
    }

    public Department create(Department deparment) {
        return departmentRepository.save(deparment);
    }

    public Department update(Department role) {
        return departmentRepository.save(role);
    }

    public void delete(Long id) {
        departmentRepository.delete(id);
    }

    public List<Department> findAll(){
    	List<Department> deparments = departmentRepository.findAll();
        return deparments;
    }

    public Page<Department> findPage(DepartmentQo deparmentQo){
       Pageable pageable = new PageRequest(deparmentQo.getPage(), deparmentQo.getSize(), new Sort(Sort.Direction.ASC, "id"));
//       PredicateBuilder pb  = new PredicateBuilder();
//       if (!StringUtils.isEmpty(deparmentQo.getName())) {
//            pb.add("name","%" + deparmentQo.getName() + "%", LinkEnum.LIKE);
//       }
       Page<Department> pages = departmentRepository.findAll(pageable);
       return pages;
    }

}
